package com.lin.ovc.pomodoro.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lin.ovc.pomodoro.entity.Plan;
import com.lin.ovc.pomodoro.entity.Task;
import com.lin.ovc.pomodoro.entity.dto.PlanForm;
import com.lin.ovc.pomodoro.entity.dto.TaskForm;
import com.lin.ovc.pomodoro.service.PlanService;
import com.lin.ovc.pomodoro.service.TaskService;
import com.lin.ovc.pomodoro.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pomodoro/plan")
public class PlanController {

    @GetMapping("/user")
    public String getUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null && authentication.isAuthenticated()) {
            return "用户: " + authentication.getName();
        } else {
            return "用户未登录";
        }
    }

    @Autowired
    PlanService planService;

    //添加清单
    @PostMapping()
    public R addPlan(@RequestBody PlanForm form) {
        planService.save(form);
        return R.ok();
    }

    //更新清单
    @PutMapping()
    public R updatePlan(TaskForm form) {
        planService.update(form);
        return R.ok();
    }

    //获取用户ID的该用户的所有清单
    @GetMapping()
    public R searchAllPlanOfUser(long id) {
        System.out.println(id);
        List<Plan> lists = planService.list(id);
        return R.ok().data("data",lists);
    }

    //删除指定的清单
    @DeleteMapping()
    public R DeletePlan(long id) {
        if(planService.removeById(id)) return R.ok();
        else return R.error();
    }

}
